package com.myworkbench.workbench.domain;

import com.myworkbench.workbench.infrastructure.DomainRegistry;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.EntityManager;
import javax.persistence.Transient;

/**
 * 命令层
 */
public class Command <T> {
    @Transient
    private  Integer id;

    public void setId(Integer id) {
        this.id = id;    }

    @Autowired
    private static EntityManager entityManager;

    protected static EntityManager getRepository() {
        if (entityManager == null) {
            entityManager = DomainRegistry.getBean(EntityManager.class);
        }
        return entityManager;
    }

    protected  EntityManager repository(){
       return  getRepository();
    }
    public void  create(){
        getRepository().persist(this);
    }
    public void  remove(){
        getRepository().remove(this);
    }
    public void  update(){
        getRepository().merge(this);
    }
    public  T get(){
        return (T) getRepository().find(this.getClass(),this.id);
    }
}
